package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.TeacherIcardInfoState;


public class TeacherIcardInfoStateRowMapper implements RowMapper<TeacherIcardInfoState>{
    private int type;
    public static final int AND_DATENAME = 1;
    public TeacherIcardInfoStateRowMapper() {
    }

    public TeacherIcardInfoStateRowMapper(int type) {
        this.type = type;
    }

    @Override
    public TeacherIcardInfoState mapRow(ResultSet rs, int rowNum) throws SQLException {
        TeacherIcardInfoState sc = null;
        
        switch (type) {
            case AND_DATENAME:
                sc = parseTeacherIcardInfoState(new TeacherIcardInfoState(), rs);
                sc.setCdate_str(SystemDateFormatUtil.formatDate(sc.getCdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                break;
            default:
                sc = parseTeacherIcardInfoState(new TeacherIcardInfoState(), rs);
        }
        
        return sc;
    }

    public static TeacherIcardInfoState parseTeacherIcardInfoState(TeacherIcardInfoState s, ResultSet rs) throws SQLException {
        return parseTeacherIcardInfoState(s, rs, null);
    }

    public static TeacherIcardInfoState parseTeacherIcardInfoState(TeacherIcardInfoState s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setTiisd(rs.getLong(su+"tiisd"));
        } catch (SQLException e) {
            s.setTiisd(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }  
        try {
            s.setCdate(rs.getDate(su+"cdate"));
        } catch (SQLException e) {
            s.setCdate(null);
        }
        try {
            s.setLate(rs.getShort(su+"late"));
        } catch (SQLException e) {
            s.setLate(null);
        }
        try {
            s.setEarly(rs.getShort(su+"early"));
        } catch (SQLException e) {
            s.setEarly(null);
        }
        try {
            s.setLeave(rs.getShort(su+"leave"));
        } catch (SQLException e) {
            s.setLeave(null);
        }
        try {
            s.setAbsenteeism(rs.getShort(su+"absenteeism"));
        } catch (SQLException e) {
            s.setAbsenteeism(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, TeacherIcardInfoState s) {
        par.put("tiisd", s.getTiisd());
        par.put("tid", s.getTid());
        par.put("cdate",s.getCdate());
        par.put("late",s.getLate());
        par.put("early", s.getEarly());
        par.put("leave", s.getLeave());
        par.put("absenteeism", s.getAbsenteeism());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, TeacherIcardInfoState s) {
        args.put("tiisd",s.getTiisd());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final TeacherIcardInfoState s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "tiisd" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(TeacherIcardInfoState s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into teacher_icard_info_state(");
        int count = 0;
        if (null != s.getTiisd()) {
            sql.append("tiisd");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid");
            count++;
        }
        if (null !=s.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null != s.getLate()) {
            sql.append("late");
            count++;
        }
        if (null != s.getEarly()) {
            sql.append("early");
            count++;
        }
        if (null != s.getLeave()) {
            sql.append("leave");
            count++;
        }
        if (null != s.getAbsenteeism()) {
            sql.append("absenteeism");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, TeacherIcardInfoState s)
        throws SQLException {
        int count = 1;
        if (null != s.getTiisd()) {
            ps.setLong(count++,s.getTiisd());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getCdate()) {
            ps.setDate(count++, (Date) s.getCdate());
        }
        if (null != s.getLate()) {
            ps.setShort(count++, s.getLate());
        }
        if (null != s.getEarly()) {
            ps.setShort(count++, s.getEarly());
        }
        if (null != s.getLeave()) {
            ps.setShort(count++, s.getLeave());
        }
        if (null != s.getAbsenteeism()) {
            ps.setShort(count++, s.getAbsenteeism());
        }  

    }
}
